//
//  HQHotMoreDetailViewController.m
//  MobileiOS
//
//  Created by 郝庆松 on 2018/7/28.
//  Copyright © 2018年 PCITC. All rights reserved.
//

#import "HQHotMoreDetailViewController.h"

@interface HQHotMoreDetailViewController ()

@end

@implementation HQHotMoreDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    _currentRect = _headerView.frame;
    _currentIndex = 0;
    
    // 样式列表
    newsView = [HQStyleListView loadTopHotView];
    newsView.frame = _listBgView.bounds;
    newsView.backgroundColor = [UIColor clearColor];
    newsView.delegate = self;
    [_listBgView addSubview:newsView];
    
    _styleID = @"";
    _newID = @"";
    
    self.title = @"热点样式详情";
    
    _dataSource = [[NSMutableArray alloc]init];
    [_styleImage setImageWithURL:[NSURL URLWithString:self.dataInfo[@"s_img"]]];
    _styleID = self.dataInfo[@"s_id"];
    _styleName.text = self.dataInfo[@"s_name"];
    _styleIntrduce.text = self.dataInfo[@"s_introduce"];
    [self test];
    
    [self calculateHeight];
}

/**
 点击了收藏
 */
-(void)collectionItem{
    [self showHudText:@""];
    [HQNetWork requetParam:[HQParam addCollection:[UserModelManager userID] s_id:_styleID] withMethod:[HQMethod addCollection] suc:^(NSDictionary *sucData) {
        [self hiddnHud];
        if([sucData[kStatus]integerValue] == 1){
            NSLog(@"%@",sucData);
            [self tostView:@"收藏成功"];
            [self test];
        }else{
            [self tostView:sucData[kInfo]];
        }
    } fail:^{
        [self hiddenErrorHud];
    }];
}

/**
 选择某个区间的时候列表随之变动
 
 @param hottopView 区间差列表
 @param dataInfo 透传的数据
 */
-(void)selectItem:(HQStyleListView *)hottopView withInfo:(NSDictionary *)dataInfo withIndexPath:(NSIndexPath *)indexPath{
    _intervalName.text = [NSString stringWithFormat:@"说明: %@",dataInfo[@"name"]];
    [_dataSource removeAllObjects];
    NSMutableArray *a = [[NSMutableArray alloc]initWithArray:dataInfo[@"zj"]];
    for(NSDictionary *dic in a){
        HQStyleObject *a = [[HQStyleObject alloc]init];
        a.mID = dic[@"m_id"];
        a.zujianCode = dic[@"m_cid"];
        a.zujianVaule = dic[@"m_material"];
        a.unit = dic[@"c_unit"];
        a.zujianName = [NSString stringWithFormat:@"%@/%@",dic[@"name"],dic[@"c_unit"]];
        [_dataSource addObject:a];
    }
    [_tableView reloadData];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataSource.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HQHotMoreDetailCell *cell = [HQHotMoreDetailCell cellWithTableView:tableView];
    cell.dataInfo = _dataSource[indexPath.row];
    return cell;
}

/******************************************************************************************************/
/*******************************************获取样式列表数据**********************************************/
/******************************************************************************************************/
/**
 获取已经创建的列表数据
 */
-(void)test{
    [self showHudText:@""];
    [HQNetWork requetParam:[HQParam stylecomFindStyle:_styleID withUid:[UserModelManager userID]] withMethod:[HQMethod findStyleCom] suc:^(NSDictionary *sucData) {
        [self hiddnHud];
        if([sucData[@"data"][@"code"] integerValue] == 1){
            NSLog(@"%@",sucData[@"data"][@"data"][@"qjc"]);
            newsView.dataSource = [[NSMutableArray alloc]initWithArray:sucData[@"data"][@"data"][@"qjc"]];
            [self calculateHeight];
            if(newsView.dataSource.count > 0){
                NSDictionary *dict = sucData[@"data"][@"data"];
                _styleName.text = dict[@"s_name"];
                if([dict[@"isgot"] integerValue] == 0){
                    self.navigationItem.rightBarButtonItem = [UIBarButtonItem barButtonItemImage:@"Collection2" withTarget:self withSelector:@selector(collectionItem) withSize:CGSizeMake(25, 25)];
                }else if( [dict[@"isgot"] integerValue] == 1){
                    self.navigationItem.rightBarButtonItem = [UIBarButtonItem barButtonItemImage:@"Collection1" withTarget:self withSelector:nil withSize:CGSizeMake(25, 25)];
                }
                _styleIntrduce.text = dict[@"s_introduce"];
                _listBgView.hidden = NO;
                [newsView selectIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]];
                // 编辑时默认选中第一条
                [newsView.collectionView selectItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0] animated:YES scrollPosition:UICollectionViewScrollPositionTop];
            }
        }else{
            [self tostView:sucData[kInfo]];
        }
    } fail:^{
        [self hiddenErrorHud];
    }];
}

/**
 根据区间差计算界面显示的高度
 */
-(void)calculateHeight{
    NSInteger count = newsView.dataSource.count;
    NSInteger line = count/3 + (count % 3 != 0 ? 1 : 0);
    _listBgViewConstraint.constant = (line*55)-5;
    CGRect frame = _currentRect;
    frame.size.height = (_currentRect.size.height + (line*55))-45;
    newsView.frame = CGRectMake(0, 0, _listBgView.bounds.size.width, _listBgView.bounds.size.height);
    _headerView.frame = frame;
    [_tableView reloadData];
}

/**
 查看大图
 
 @param sender 按钮
 */
- (IBAction)lookBigImage:(id)sender {
    if(_styleImage.image){
        HQBigImageViewController *bigImageVc = [[HQBigImageViewController alloc]init];
        bigImageVc.imageData = _styleImage.image;
        [self presentViewController:bigImageVc animated:YES completion:nil];
    }else{
        [self tostView:@"暂无图片"];
    }
}

@end
